var util = require("../util"), constants = require("./constants"), TIMING_KEYS = constants.TIMING_KEYS;

module.exports = function() {
    var t = util.win || {}, e = t.performance;
    if (!e || "object" != typeof e || "function" != typeof e.getEntriesByType) return null;
    var n = {}, i = e.timing || {}, r = e.getEntriesByType("resource") || [];
    if (n.begin = i[TIMING_KEYS[1]] || Date.now(), "function" == typeof t.PerformanceNavigationTiming) {
        var o = e.getEntriesByType("navigation")[0];
        o && (i = o);
    }
    return util.each({
        dom: [ 10, 8 ],
        load: [ 14, 1 ]
    }, function(t, e) {
        var r = i[TIMING_KEYS[t[1]]], o = i[TIMING_KEYS[t[0]]];
        if (r > 0 && o > 0) {
            var a = Math.round(o - r);
            a >= 0 && a < 6e5 && (n[e] = a);
        }
    }), n.res = JSON.stringify(r), n;
};